﻿using AutoMapper;
using PowerHub.MES.Common.NumberingCenter;
using PowerHub.MES.DTO.Shopfloor.DTO;
using PowerHub.MES.DTO.Shopfloor.QueryDTO;
using PowerHub.MES.EFBase;
using PowerHub.MES.Entities.Shopfloor;
using PowerHub.MES.Entities;
using PowerHub.MES.IRepository.IShopfloor;
using PowerHub.MES.IServices.IShopfloor;
using PowerHub.MES.Jwt;
using PowerHub.MES.ReturnEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PowerHub.MES.Entities.ProcessRoute;
using PowerHub.MES.IServices.IProcessRoute;
using PowerHub.MES.DTO.ProcessRoute.QueryDto;
using PowerHub.MES.DTO.ProcessRoute.DTO;
using PowerHub.MES.IRepository.IProcessRoute;

namespace PowerHub.MES.Services.ProcessRoute
{
    /// <summary>
    /// 工序接口实现层
    /// </summary>
    public class ProcessRoute_ProcessServices : BaseServices<ProcessRoute_Process>, IProcessRoute_ProcessServices
    {
        private IProcessRoute_ProcessRepository _iProcessRoute_ProcessRepository;
        public UserServices _userServices;
        private IMapper _mapper;
        private static Random random = new Random();

        public ProcessRoute_ProcessServices(PowerHubMesContext powerHubMesContext, IProcessRoute_ProcessRepository iProcessRoute_ProcessRepository, UserServices userServices, IMapper mapper) : base(powerHubMesContext)
        {
            _iProcessRoute_ProcessRepository = iProcessRoute_ProcessRepository;
            _userServices = userServices;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取工序列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationPageResult<List<ProcessRoute_Process>>> GetProcessRoute_ProcessPageListAsync(QueryProcessRoute_ProcessDTO query)
        {
            var ss = _userServices.UserId;
            var UserName = _userServices.UserName;
            var Role = _userServices.Role;
            return await _iProcessRoute_ProcessRepository.GetProcessRoute_ProcessPageListAsync(query);
        }

        /// <summary>
        /// 添加工序
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> CreateProcessRoute_ProcessAsync(ProcessRoute_ProcessDTO query)
        {
            ProcessRoute_Process foundation = await GetEntityAsync(d => d.ProcessName.Equals(query.ProcessName) && d.IsDel == 0);
            if (foundation != null)
            {
                throw new Exception("你输入的工序名称已存在！");
            }
            //实体转换
            var ProcessRoute_Process = _mapper.Map<ProcessRoute_Process>(query);
            ProcessRoute_Process.Id = Guid.NewGuid().ToString();
            ProcessRoute_Process.ProcessNo = string.IsNullOrEmpty(query.ProcessNo) || query.ProcessNo.Contains(" ") ? GenerateBasicDataNO.GenerateCode(query.ProcessName) : query.ProcessNo;
            ProcessRoute_Process.Creator = _userServices.UserName;

            return await _iProcessRoute_ProcessRepository.CreateProcessRoute_ProcessAsync(ProcessRoute_Process);
        }

      

        /// <summary>
        /// 批量删除工序
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> DeletionProcessRoute_ProcessAsync(List<string>? ids)
        {
            if (ids.Count < 1)
            {
                throw new Exception("请先选择数据！");
            }
            return await _iProcessRoute_ProcessRepository.DeletionProcessRoute_ProcessAsync(ids);
        }


        /// <summary>
        /// 更新工序
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> UpdateProcessRoute_ProcessAsync(ProcessRoute_ProcessDTO query)
        {
            if (query == null)
            {
                throw new Exception("请先选择数据！");
            }

            //根据id获取工序信息
            ProcessRoute_Process ProcessRoute_Process = await GetEntityAsync(d => d.Id.Equals(query.Id));
            if (ProcessRoute_Process == null)
            {
                throw new Exception("更新工序信息发生了异常,请刷新页面后试试");
            }
            ProcessRoute_Process.ProcessName = query.ProcessName;
            ProcessRoute_Process.ProcessExplain = query.ProcessExplain;
            ProcessRoute_Process.Remark = query.Remark;
            ProcessRoute_Process.Updator = _userServices.UserName;


            return await _iProcessRoute_ProcessRepository.UpdateProcessRoute_ProcessAsync(ProcessRoute_Process);

        }
    }
}
